package com.spboot.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spboot.entity.User;
import com.spboot.page.PageInfo;
import com.spboot.service.RoleService;
import com.spboot.service.UserService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author guocb
 * @since 2019-12-25
 */
@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	UserService userService;

	@Autowired
	RoleService roleService;

	//权限注解
	@RequiresPermissions("user_list")
	@RequestMapping("/list")
	public String list(@RequestParam(defaultValue = "1", required = false) Long pageno, String userName,
			Integer userRole, Model model) {
		// 条件构造器
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		if (StringUtils.isNotEmpty(userName)) {
			queryWrapper.likeRight(User.USERNAME, userName);
		}
		if (userRole != null && userRole != 0) {
			queryWrapper.eq(User.USERROLE, userRole);
		}
		//...
		queryWrapper.eq(User.ISDELETE, 0);//未删除
		// 分页必须开启分页插件
		PageInfo<User> pageInfo = new PageInfo<>(pageno, 6);
		pageInfo = (PageInfo<User>) userService.selectPageUser(pageInfo, queryWrapper);
		
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("roleList", roleService.list());
		return "userlist";
	}

	@RequiresPermissions("user_toadd")
	@GetMapping("/useradd")
	public String useradd(Model model) {

		model.addAttribute("roleList", roleService.list());
		return "useradd";
	}

	@RequiresPermissions("user_add")
	@PostMapping("/add")
	public String add(User user, Model model) {

		//密码处理
		String newPaswword = new SimpleHash("md5", user.getUserPassword(), user.getUserCode(), 2).toString();
		user.setUserPassword(newPaswword);
		
		boolean f = userService.save(user);
		if (f) {

			return "redirect:list";
		}

		model.addAttribute("roleList", roleService.list());
		return "useradd";
	}

	@RequiresPermissions("user_get")
	@RequestMapping("get")
	public String get(Long id, Model model) {
		User user = userService.getById(id);
		model.addAttribute("user", user);

		model.addAttribute("roleList", roleService.list());
		return "useredit";
	}
	@RequiresPermissions("user_update")
	@PostMapping("/edit")
	public String edit(User user, Model model) {
		//密码处理
		String newPaswword = new SimpleHash("md5", user.getUserPassword(), user.getUserCode(), 2).toString();
		user.setUserPassword(newPaswword);
				
		boolean f = userService.updateById(user);
		if (f) {

			return "redirect:list";
		}

		return "redirect:get?id=" + user.getId();
	}
	@RequiresPermissions("user_del")
	@ResponseBody
	@RequestMapping("del")
	public Boolean del(Integer id){
		//逻辑删除 更新删除字段为已删除的值
		return userService.removeById(id);
	}
}
